package controller

import (
	"afupanel/dto"
	"afupanel/service"
	"errors"
	"github.com/gin-gonic/gin"
	"net/http"
)

/**
返回失败，带消息
*/
func ReturnFailed(msg string, c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"status": false,
		"code":   -1,
		"msg":    msg,
	})
}

/**
返回失败，带消息code
*/
func ReturnFailedWithCodeMsg(code int, msg string, c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"status": false,
		"code":   code,
		"msg":    msg,
	})
}

/**
返回成功
*/
func ReturnSuccess(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"status": true,
		"code":   200,
		"msg":    "操作成功",
	})
}

/**
返回成功带数据
*/
func ReturnSuccessWithData(data interface{}, c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"status": true,
		"code":   200,
		"msg":    "操作成功",
		"data":   data,
	})
}

func CheckSession(c *gin.Context) (err error) {
	h := dto.HeaderReq{}
	if err := c.ShouldBindHeader(&h); err != nil {
		return err
	}
	if isLogin, err := service.IsLogin(h.Token); err != nil {
		return err
	} else {
		if !isLogin {
			return errors.New("Session过期")
		}
	}
	return nil
}

func GetClientIP(c *gin.Context) (ip string) {
	ip = c.ClientIP()
	return ip
}
